package com.winit.core.host.code;

import com.winit.common.util.ObjectToString;
import com.winit.core.host.code.beans.OperateParam;
import com.winit.core.host.code.beans.OperateType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangjian on 16/7/22.
 * 代码操作,包括检出.更新.提交
 */
public class CodeOperator {

    private static final Logger logger = LoggerFactory.getLogger(CodeOperator.class);

    private List<Listener> listeners = new ArrayList<>();

    private OperateParam operateParam;

    CodeOperator(OperateParam operateParam,Listener listener){
        this.operateParam = operateParam;
        this.listeners.add(listener);
    }

    public Map<String,String> doOperate(){
        Map<String,String> map = new HashMap<>();
        if(this.listeners.size() == 0){
            map.put("code","3");
            map.put("desc","代码操作,listeners为空");
            logger.info("执行代码操作,operateParam="+ ObjectToString.getString(this.operateParam)+",map="+map);
            return map;
        }
        for (Listener listener:this.listeners){
            switch (this.operateParam.getOperateType()){
                case CHECK_OUT:
                    map = listener.checkOut(this.operateParam);
                    break;
                case COMMIT:
                    map = listener.commit(this.operateParam);
                    break;
                case UPDATE:
                    map = listener.update(this.operateParam);
                    break;
                default:
                    map.put("code","3");
                    map.put("desc","代码操作,参数错误");
                    break;
            }
        }
        logger.info("执行代码操作,operateParam="+ ObjectToString.getString(this.operateParam)+",map="+map);
        return map;
    }

    public interface Listener {
        Map<String,String> checkOut(OperateParam operateParam);
        Map<String,String> commit(OperateParam operateParam);
        Map<String,String> update(OperateParam operateParam);
    }


    public static void main(String[] args) {
        OperateParam operateParam = new OperateParam();
        operateParam.setRemotePath("svn://192.168.56.111/WJ/winit/patch1");
        operateParam.setLocalPath("/Users/wangjian/Desktop/test");
        operateParam.setOperateType(OperateType.UPDATE);

        CodeOperator codeOperator = new CodeOperator(operateParam,new SvnOperatorListener());
        codeOperator.doOperate();
    }

}
